package com.marvels.product.param.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.product.param.dao.McptCustomerTypeParamDao;
import com.marvels.product.param.dao.McptProductMapper;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.ProductItemService;
import com.marvels.product.productmanage.dao.McptChannelDao;
import com.marvels.product.productmanage.dao.ProductTypeDao;
import com.marvels.product.productmanage.dto.McptProductInfoDTO;
import com.marvels.product.productmanage.dto.McptProductMappedDto;
import com.marvels.product.productmanage.dto.McptProductTypeDto;
@Service
public class ProductItemServiceImpl implements ProductItemService {
	
	@Autowired
	private McptProductMapper mapper;
	@Autowired
	private McptChannelDao mcptChannelDao;
	@Autowired
	private ProductTypeDao productTypeDao;
	@Autowired
	private McptCustomerTypeParamDao mcptCustomerTypeParamDao;
	
	@Override
	public PageInfo<McptProduct> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
		
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptProduct>  proList = mapper.getListByParam(param);
		
		PageInfo<McptProduct> pageInfo = new PageInfo<McptProduct>(proList);
	    
		return pageInfo;
	}

	@Override
	public PageInfo<McptProduct> getPageInfoByVersion(Map<String, Object> param) throws MarvelsBusinessException {
		
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptProduct>  proList = mapper.getPageInfoByVersion(param);
		
		PageInfo<McptProduct> pageInfo = new PageInfo<McptProduct>(proList);
	    
		return pageInfo;
	}

	@Override
	public void updateStatus(McptProduct dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		mapper.updateStatus(dto);
	}

	@Override
	public List<Map<String, String>> getPtFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getPtFeeMapList();
	}

	@Override
	public List<Map<String, String>> getMiscFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getMiscFeeMapList();
	}


	@Override
	public List<Map<String, String>> getMngFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getMngFeeMapList();
	}

	@Override
	public List<Map<String, String>> getPreFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getPreFeeMapList();
	}

	@Override
	public List<Map<String, String>> getLateFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getLateFeeMapList();
	}

	@Override
	public List<Map<String, String>> getPenaltyMapList() {
		// TODO Auto-generated method stub
		return mapper.getPenaltyMapList();
	}

	@Override
	public List<Map<String, String>> getConsultFeeMapList() {
		// TODO Auto-generated method stub
		return mapper.getConsultFeeMapList();
	}

	@Override
	public void deleteByPrimaryKey(Long productId) {
		// TODO Auto-generated method stub
		mapper.deleteByPrimaryKey(productId);
	}

	@Override
	@Transactional
	public void updateBatchItem(List<McptProduct> dtoList) throws MarvelsBusinessException {
		if(dtoList!=null&& dtoList.size()>0)
		mapper.updateBatchItem(dtoList);
	}

	@Override
	public void save(McptProduct dto) throws MarvelsBusinessException {
		dto.setIsValid((byte) 0);
		dto.setProductCode(MarvelsUtils.generateUUIDCode("PT"));
		dto.setProductName(dto.getProductTypeName()+"-"+dto.getChannel()+"-"+dto.getTenor()+"-"+dto.getGrade());
		// TODO Auto-generated method stub
		mapper.save(dto);
	}

	@Override
	public List<Map<Integer, String>> getAmortMethodMapList() {
		// TODO Auto-generated method stub
		return mapper.getAmortMethodMapList();
	}

	@Override
	public List<Map<Integer, String>> getRepaySeqMapList() {
		// TODO Auto-generated method stub
		return mapper.getRepaySeqMapList();
	}

	@Override
	public McptProduct getByProductCode(McptProduct dto) throws MarvelsBusinessException{
		if(dto == null){
			throw new MarvelsBusinessException("参数为空");
		}
		return mapper.selectByProductCode(dto);
	}

	@Override
	public List<McptProductMappedDto> selectProductMappedInfo(McptProductMappedDto mcptProductMappedDto) {
		return mapper.selectProductMappedInfo(mcptProductMappedDto);
	}

	@Override
	public Map<String, Object> initDataMap(McptProductTypeDto productTypeDto) throws MarvelsBusinessException {
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("ptFeeMap", mapper.getPtFeeMapList());  //平台费
		dataMap.put("miscFeeMap", mapper.getMiscFeeMapList());   //杂费
		dataMap.put("consultFeeMap", mapper.getConsultFeeMapList());   //服务费
		dataMap.put("mngFeeMap", mapper.getMngFeeMapList());   //管理费
		dataMap.put("preFeeMap", mapper.getPreFeeMapList());   //提前结清费
		dataMap.put("lateFeeMap", mapper.getLateFeeMapList());   //滞纳金
		dataMap.put("penaltyFeeMap", mapper.getPenaltyMapList());   //违约金
		
		dataMap.put("amortMethodList", mapper.getAmortMethodMapList());   //还款计算方式
		dataMap.put("repaySeqList", mapper.getRepaySeqMapList());      //还款顺序
		dataMap.put("channels", mcptChannelDao.selectChannel(productTypeDto));      //渠道
		dataMap.put("terms", productTypeDao.selectTermByProductCode(productTypeDto));//期限
		dataMap.put("grades", mcptCustomerTypeParamDao.selectAllType());      //等级

		return dataMap;
	}

	@Override
	public List<McptProductInfoDTO> selectProductInfo(McptProductInfoDTO mcpdto) throws MarvelsBusinessException {
		if(mcpdto == null){
			throw new MarvelsBusinessException("参数为空");
		}
		return mapper.selectProductInfo(mcpdto);
	}
	
	
	
}
